﻿using System;
using System.Collections.Generic;

namespace Web.Class
{
    /// <summary>
    ///     回调结果
    /// </summary>
    public class CallbackResult
    {
        /// <summary>
        ///     返回数据
        /// </summary>
        private readonly List<string> values = new List<string>();

        public CallbackResult()
        {
        }

        public CallbackResult(string optype)
        {
            OpType = optype;
        }

        /// <summary>
        ///     当前操作类型
        /// </summary>
        public string OpType { get; set; }

        /// <summary>
        ///     是否成功
        /// </summary>
        public bool IsSuccess { get; set; }

        /// <summary>
        ///     特别标识,用于在成功或失败时有多种不同情况做对应不同处理时需要
        /// </summary>
        public string SpecialIndetity { get; set; }

        /// <summary>
        ///     是否回调完成后弹消息
        /// </summary>
        public bool IsAlertMsg { get; set; }

        /// <summary>
        ///     消息
        /// </summary>
        public string Msg { get; set; }

        /// <summary>
        ///     添加返回数据
        /// </summary>
        public void AddValue(params object[] values)
        {
            foreach (var item in values)
                this.values.Add(Convert.ToString(item));
        }

        /// <summary>
        ///     添加返回数据
        /// </summary>
        public void AddValue(params string[] values)
        {
            foreach (var item in values)
                this.values.Add(item);
        }

        public void ClearValues()
        {
            values.Clear();
        }

        /// <summary>
        ///     生成返回字符串 格式为:OpType|IsSuccess|SpecialIndetity|IsAlertMsg-Msg|Values
        ///     前台js通过split分割数据后
        ///     arr[0]:type
        ///     arr[1]:IsSuccess(true or false)
        ///     arr[2]: SpecialIndetity
        ///     arr[3].split('-')[0]:IsAlertMsg(true or false)
        ///     arr[3].split('-')[1]:Msg
        ///     arr[4]-arr[n]:所需数据
        /// </summary>
        public override string ToString()
        {
            var rs = new List<string>();
            rs.Add(OpType);
            rs.Add(IsSuccess.ToString());
            //rs.Add(string.IsNullOrEmpty(this.SpecialIndetity) ? "0" : this.SpecialIndetity);
            rs.Add(SpecialIndetity);
            rs.Add(IsAlertMsg + "-" + (string.IsNullOrEmpty(Msg) ? " " : Msg));
            for (var i = 0; i < values.Count; i++)
                rs.Add(values[i]);
            return string.Join("|", rs);
        }
    }
}